<div id="show_next">
<?php
	if ( $node->cs_event_type == "Upcoming" ) $meta_compare = ">=";
	else if ( $node->cs_event_type == "Past" ) $meta_compare = "<";

	$row_cat = $wpdb->get_row("SELECT * from ".$wpdb->prefix."terms WHERE slug = '" . $node->cs_event_category ."'" );
	//if ( empty ($row_cat->name) or $row_cat->name == "" ) $row_cat->name = "";
	if ( isset($_GET['filter_category']) ) $filter_category = $_GET['filter_category'];
	else $filter_category = $row_cat->slug;

	if ( empty($_GET['page_id_all']) ) $_GET['page_id_all'] = 1;

		if ( $node->cs_event_type == "All" ) {
			$args = array(
				'posts_per_page'			=> "-1",
				'post_type'					=> 'events',
				'event-category'			=> "$filter_category",
				'post_status'				=> 'publish',
				'orderby'					=> 'meta_value',
				'order'						=> 'ASC',
				);
		}
		else {
			$args = array(
				'posts_per_page'			=> "-1",
				'post_type'					=> 'events',
				'event-category'			=> "$filter_category",
				'post_status'				=> 'publish',
				'meta_key'					=> 'cs_event_to_date',
				'meta_value'				=> date('Y-m-d'),
				'meta_compare'				=> $meta_compare,
				'orderby'					=> 'meta_value',
				'order'						=> 'ASC',
				);
		}
		query_posts($args);
		$count_post = 0;
			while ( have_posts()) : the_post();
				$count_post++;
			endwhile;
		
		if ( $node->cs_event_pagination == "Single Page" or $node->cs_event_filterables == "On" ) $node->cs_event_per_page = -1;
?>
    <h1 class="heading"><?php echo $node->cs_event_title?> (<?php echo $node->cs_event_type?> Events)</h1>
    <div class="cat-select">
        <ul>
            <li><h5>Select Events Category</h5></li>
            <li>
                <form action="" method="get">
                    <input type="hidden" name="page_id" value="<?php if (isset($_GET['page_id'])) echo $_GET['page_id']?>" />
                    <select name="filter_category" onchange="this.form.submit()">
                        <option><?php echo $row_cat->name?></option>
                        <?php
                            $categories = get_categories( array('child_of' => "$row_cat->term_id", 'taxonomy' => 'event-category', 'hide_empty' => 0) );
                            foreach ($categories as $category) {
                        ?>
                            <option <?php if($filter_category==$category->cat_name) echo "selected";?> ><?php echo $category->cat_name?></option>
                        <?php
                            }
                        ?>
                    </select>
                </form>
            </li>
        </ul>
    </div>

	<?php
    if( $node->cs_event_view == "List View" ) {
	?>
        <div class="in-sec in-sec-nopad">
            <!-- Timeline Start -->
            <div id="tab-timeline">
                <ul class="timeline">
                    <?php
					if ( $node->cs_event_type == "All" ) {
						$args = array(
							'posts_per_page'			=> "$node->cs_event_per_page",
							'paged'						=> $_GET['page_id_all'],
							'post_type'					=> 'events',
							'event-category'			=> "$filter_category",
							'post_status'				=> 'publish',
							'orderby'					=> 'meta_value',
							'order'						=> 'ASC',
							);
					}
					else {
						$args = array(
							'posts_per_page'			=> "$node->cs_event_per_page",
							'paged'						=> $_GET['page_id_all'],
							'post_type'					=> 'events',
							'event-category'			=> "$filter_category",
							'post_status'				=> 'publish',
							'meta_key'					=> 'cs_event_to_date',
							'meta_value'				=> date('Y-m-d'),
							'meta_compare'				=> $meta_compare,
							'orderby'					=> 'meta_value',
							'order'						=> 'ASC',
							);
					}
                    query_posts($args);
                    if ( have_posts() <> "" ) {
                        while ( have_posts() ): the_post();
                            $cs_event_meta = get_post_meta($post->ID, "cs_event_meta", true);
                            if ( $cs_event_meta <> "" ) {
                                $cs_event_meta = new SimpleXMLElement($cs_event_meta);
                            }
                    ?>
                            <li>
                                <div class="date">
                                    <span>&nbsp;</span>
                                    <h6 class="colr">
                                        <?php
                                            $event_from_date = get_post_meta($post->ID, "cs_event_from_date", true);
                                            $event_to_date = get_post_meta($post->ID, "cs_event_to_date", true);
                                            echo date( get_option("date_format"), strtotime($event_from_date) );
											if ( $event_from_date < $event_to_date ) {
												echo "<br /> To <br />";
												echo date( get_option("date_format"), strtotime($event_to_date) );
											}
                                        ?>
                                    </h6>
                                </div>
                                <div class="desc">
                                    <div class="desc-in">
                                        <span class="pointer">&nbsp;</span>
                                        <div class="thumb">
                                            <a href="<?php echo get_permalink()?>">
                                                <?php
                                                    $image_id = get_post_thumbnail_id ( $post->ID );
                                                    if ( $image_id <> "" ) {
                                                        $image_url = cs_attachment_image_src($image_id, 120, 95);
                                                        echo "<img src='".$image_url."' />";
                                                    }
                                                    else {
                                                        echo "<img width='120' height='95' src='".get_template_directory_uri()."/images/no_image.jpg' />";
                                                    }
                                                ?>	
                                            </a>
                                        </div>
                                        <div class="txt">
                                            <h5>
                                                <a href="<?php echo get_permalink()?>">
                                                    <?php echo substr(get_the_title(), 0, 50);  if ( strlen(get_the_title()) > 50 ) echo "..."; ?>
                                                </a>
                                            </h5>
                                            <p>
                                                <?php
                                                    $cs_news_excerpt_db = $node->cs_event_excerpt;
                                                    if ( $post->post_excerpt <> "" ) $get_the_excerpt = $post->post_excerpt;
                                                    else $get_the_excerpt = $post->post_content;
                                                        $get_the_excerpt = trim(preg_replace('/<a[^>]*>(.*)<\/a>/iU','', $get_the_excerpt ));
                                                        echo substr($get_the_excerpt, 0, "$cs_news_excerpt_db");
                                                        if ( strlen( $get_the_excerpt ) > "$cs_news_excerpt_db" ) {
                                                            echo '... <a href="'.get_permalink().'"> Read more</a>';
                                                        }
                                                ?>
                                            </p>
                                        </div>
                                            <div class="map-sec active" id="mapctr<?php echo $post->ID.$counter_gal;?>" style="display:none;">
                                                <a onClick="hidefnc(<?php echo $post->ID.$counter_gal;?>);" class="closemap">&nbsp;</a>
                                                <div class="mapdiv<?php echo $post->ID.$counter_gal;?>" id="map_canvas<?php echo $post->ID.$counter_gal;?>" style="display:none; height:200px;">
                                                </div>
                                            </div>
                                            <div class="gig-opts">
                                                <?php 
                                                if($node->cs_event_time == 'Yes') {
                                                    echo "<h6 class='time'>";
                                                    if ( $cs_event_meta->event_all_day == "" ) {
                                                        echo $cs_event_meta->event_start_time . " &ndash; " . $cs_event_meta->event_end_time;
                                                    }
                                                    else echo "All Day";
                                                    echo "</h6>";
                                                }
                                                ?>
                                                <?php if($cs_event_meta->event_booking_url <> ""){ ?>
                                                    <a href="<?php echo $cs_event_meta->event_booking_url;?>" target="_blank">BUY TICKETS</a>
                                                <?php }?>
                                                <?php
													$cs_event_loc = get_post_meta($cs_event_meta->event_address, "cs_event_loc_meta", true);
													if ( $cs_event_loc <> "" ) {
														$xmlObject = new SimpleXMLElement($cs_event_loc);
															$event_loc_lat = $xmlObject->event_loc_lat;
															$event_loc_long = $xmlObject->event_loc_long;
															$event_loc_zoom = $xmlObject->event_loc_zoom;
													}
													else {
														$event_loc_lat = '';
														$event_loc_long = '';
														$event_loc_zoom = '';
													}
                                                ?>
                                                <a onClick="showhide(<?php echo $event_loc_lat?>, <?php echo $event_loc_long?>, <?php echo $event_loc_zoom;?>, <?php echo $post->ID.$counter_gal;?>);" class="location"><?php echo get_the_title( "$cs_event_meta->event_address" )?></a>
                                            </div>
                                        </div>
                                    </div>
                                </li>
                    <?php
                        endwhile;
                    }
                    ?>
                </ul>
                        <div class="clear"></div>
                            <?php
                            // pagination start
                                if ( $node->cs_event_pagination == "Show Pagination" and $node->cs_event_per_page > 0 and $node->cs_event_filterables == "No" ) {
                                    echo "<div class='in-sec'><ul class='pagination'>";
                                        $qrystr = '';
                                        if ( isset($_GET['page_id']) ) $qrystr = "&page_id=".$_GET['page_id'];
                                        if ( isset($_GET['filter_category']) ) $qrystr .= "&filter_category=".$_GET['filter_category'];
                                    echo cs_pagination($count_post, $node->cs_event_per_page, $qrystr);
                                    echo "</ul></div>";
                                }
                            // pagination end
                            ?>
                    </div>
                    <!-- Timeline End -->
            <div class="clear"></div>
        </div>

		<script type="text/javascript">
            function showhide(lat,long,zoom_get,map_canvas){
                $(".mapdiv"+map_canvas).slideToggle();
                hidefnc(map_canvas);
                $(".mapdiv"+map_canvas).html();
                initialize(lat, long, zoom_get, map_canvas);
                $("#maps"+map_canvas).addClass("active");
            }
            function hidefnc(map_canvas){
                $("#mapctr"+map_canvas).slideToggle();
            }
        </script>
        <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=true"></script>
		<script type="text/javascript">
          var map;
          function initialize(lat, long, zoom_get, map_canvas) {
              var myLatLng = new google.maps.LatLng(lat, long)
                    //Initialize MAP
              var myOptions = {
                  zoom: zoom_get,
                  center: myLatLng,
                  disableDefaultUI: true,
                  zoomControl: true,			  
                  mapTypeId: google.maps.MapTypeId.ROADMAP
                };
                map = new google.maps.Map(document.getElementById('map_canvas'+map_canvas),myOptions);
              //End Initialize MAP
              //Set Marker
                var marker = new google.maps.Marker({
                  position: map.getCenter(),
                  map: map
                });
              marker.getPosition();
              google.maps.event.trigger(map, 'resize');
              //End marker
              //Set info window
              var infowindow = new google.maps.InfoWindow({
               content: '',
               position: myLatLng
              });
              infowindow.open(map);
              $("#maps").addClass("active"); 
               //end info window
          }
        
        </script>
<?php
	}
	else {
		if ( $node->cs_event_type == "All" ) {
			$args = array(
				'posts_per_page'			=> "-1",
				'post_type'					=> 'events',
				'event-category'			=> "$filter_category",
				'post_status'				=> 'publish',
				'orderby'					=> 'meta_value',
				'order'						=> 'ASC',
				);
		}
		else {
			$args = array(
				'posts_per_page'			=> "-1",
				'post_type'					=> 'events',
				'event-category'			=> "$filter_category",
				'post_status'				=> 'publish',
				'meta_key'					=> 'cs_event_to_date',
				'meta_value'				=> date('Y-m-d'),
				'meta_compare'				=> $meta_compare,
				'orderby'					=> 'meta_value',
				'order'						=> 'ASC',
				);
		}
		query_posts($args);
			if ( have_posts() <> "" ) {
				while ( have_posts() ): the_post();
					$cs_event_meta = get_post_meta($post->ID, "cs_event_meta", true);
					$event_from_date = get_post_meta($post->ID, "cs_event_from_date", true);
					if ( $cs_event_meta <> "" ) {
						$cs_event_meta = new SimpleXMLElement($cs_event_meta);
					}
					$aaa[] = array(
						'title' => substr(get_the_title(), 0, 35).'....',
						'start' => date("Y-m-d", strtotime($event_from_date)),
						'url' => get_permalink()
					);
				endwhile;
			} // have Post Condition
?>
        <!--Calendar View start-->
            <link rel='stylesheet' type='text/css' href='<?php echo get_template_directory_uri()?>/css/fullcalendar.css' />
            <script type='text/javascript' src='<?php echo get_template_directory_uri()?>/scripts/fullcalendar/fullcalendar.min.js'></script>
            <script type='text/javascript'>
                $(document).ready(function() {
                    $('#calendar').fullCalendar({
                        editable: false,
                        events: <?php echo json_encode( $aaa );?>,
                        eventDrop: function(event, delta) {
                            alert(event.title + ' was moved ' + delta + ' days\n' +
                                '(should probably update your database)');
                        },
                        loading: function(bool) {
                            if (bool) $('#loading').show();
                            else $('#loading').hide();
                        }
                    });
                });
            </script>
            <div id='loading' style='display:none'>loading...</div>
            <div class="in-sec in-sec-nopad">
                <div id='calendar'></div>
            </div>
        <!--Calendar View end -->
<?php }?>
</div>